package com.bluetrum.devicemanager.cmd.payloadhandler;

import androidx.annotation.NonNull;

import com.bluetrum.devicemanager.models.LeAudioBisItem;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

public class LeAudioBisListDeletePayloadHandler extends PayloadHandler<List<LeAudioBisItem>> {

    public LeAudioBisListDeletePayloadHandler(@NonNull byte[] payload) {
        super(payload);
    }

    @Override
    public List<LeAudioBisItem> call() {
        final byte[] payload = getPayload();

        List<LeAudioBisItem> list = new ArrayList<>();

        ByteBuffer bb = ByteBuffer.wrap(payload)
                .order(ByteOrder.LITTLE_ENDIAN);

        int size = bb.get();
        for (int i = 0; i < size; i++) {
            byte type = bb.get();
            int length = bb.get();
            if (type == LeAudioBisItem.TYPE_IDENTIFIER && length == 4) {
                int identifier = bb.getInt();
                LeAudioBisItem item =
                        new LeAudioBisItem(
                                identifier,
                                null,
                                null,
                                null,
                                null,
                                null
                        );
                list.add(item);
            }
        }

        return list;
    }
}
